package com.soft.controller;

import com.soft.pojo.Emp;
import com.soft.pojo.PageBean;
import com.soft.service.WorkService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ruoxi
 * @create 2021-12-25 18:59
 */
@WebServlet("/showAllWorkerByPage")
public class ShowAllWorkerByPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        WorkService workService = new WorkService();
        PageBean<Emp> empPageBean = new PageBean<>();

        int currentPage = 1;
        String currentPage_ = req.getParameter("currentPage");
        if (currentPage_ != null) {
            currentPage = Integer.parseInt(currentPage_);
        }

        int currentCount = 10;

        empPageBean.setCurrentPage(currentPage);
        empPageBean.setCurrentCount(currentCount);
        empPageBean.setTotalCount(workService.findEmpCount());
        empPageBean.setTotalPage((int) Math.ceil(workService.findEmpCount() * 1.0 / currentCount));
        empPageBean.setList(workService.findEmpByPage((currentPage - 1) * currentCount, currentCount));

        req.setAttribute("workPg", empPageBean);
        req.getRequestDispatcher("daily.jsp").forward(req, resp);
    }
}
